
USE [Ticket]
GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.loaive_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.loaive_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.loaive_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the loaive table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.loaive_Get_List

AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[LoaiVeID],
					[TenVe],
					[MoTa]
				FROM
					[dbo].[loaive]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.loaive_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.loaive_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.loaive_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the loaive table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.loaive_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [LoaiVeID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([LoaiVeID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [LoaiVeID]'
				SET @SQL = @SQL + ' FROM [dbo].[loaive]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[LoaiVeID], O.[TenVe], O.[MoTa]
				FROM
				    [dbo].[loaive] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[LoaiVeID] = PageIndex.[LoaiVeID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[loaive]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.loaive_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.loaive_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.loaive_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the loaive table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.loaive_Insert
(

	@LoaiVeId int    OUTPUT,

	@TenVe nvarchar (MAX)  ,

	@MoTa nvarchar (MAX)  
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				INSERT INTO [dbo].[loaive]
					(
					[TenVe]
					,[MoTa]
					)
				VALUES
					(
					@TenVe
					,@MoTa
					)
				
				-- Get the identity value
				SET @LoaiVeId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.loaive_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.loaive_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.loaive_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the loaive table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.loaive_Update
(

	@LoaiVeId int   ,

	@TenVe nvarchar (MAX)  ,

	@MoTa nvarchar (MAX)  
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[loaive]
				SET
					[TenVe] = @TenVe
					,[MoTa] = @MoTa
				WHERE
[LoaiVeID] = @LoaiVeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.loaive_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.loaive_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.loaive_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the loaive table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.loaive_Delete
(

	@LoaiVeId int   
)
AS


				DELETE FROM [dbo].[loaive] WITH (ROWLOCK) 
				WHERE
					[LoaiVeID] = @LoaiVeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.loaive_GetByLoaiVeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.loaive_GetByLoaiVeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.loaive_GetByLoaiVeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the loaive table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.loaive_GetByLoaiVeId
(

	@LoaiVeId int   
)
AS


				SELECT
					[LoaiVeID],
					[TenVe],
					[MoTa]
				FROM
					[dbo].[loaive]
				WHERE
					[LoaiVeID] = @LoaiVeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.loaive_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.loaive_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.loaive_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the loaive table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.loaive_Find
(

	@SearchUsingOR bit   = null ,

	@LoaiVeId int   = null ,

	@TenVe nvarchar (MAX)  = null ,

	@MoTa nvarchar (MAX)  = null 
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [LoaiVeID]
	, [TenVe]
	, [MoTa]
    FROM
	[dbo].[loaive]
    WHERE 
	 ([LoaiVeID] = @LoaiVeId OR @LoaiVeId IS NULL)
	AND ([TenVe] = @TenVe OR @TenVe IS NULL)
	AND ([MoTa] = @MoTa OR @MoTa IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [LoaiVeID]
	, [TenVe]
	, [MoTa]
    FROM
	[dbo].[loaive]
    WHERE 
	 ([LoaiVeID] = @LoaiVeId AND @LoaiVeId is not null)
	OR ([TenVe] = @TenVe AND @TenVe is not null)
	OR ([MoTa] = @MoTa AND @MoTa is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.phim_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.phim_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.phim_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the phim table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.phim_Get_List

AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[PhimID],
					[TenPhim],
					[LichChieuBatDau],
					[MoTa],
					[TrangThai],
					[LichChieuKetThuc]
				FROM
					[dbo].[phim]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.phim_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.phim_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.phim_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the phim table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.phim_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [PhimID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([PhimID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [PhimID]'
				SET @SQL = @SQL + ' FROM [dbo].[phim]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[PhimID], O.[TenPhim], O.[LichChieuBatDau], O.[MoTa], O.[TrangThai], O.[LichChieuKetThuc]
				FROM
				    [dbo].[phim] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[PhimID] = PageIndex.[PhimID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[phim]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.phim_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.phim_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.phim_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the phim table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.phim_Insert
(

	@PhimId int    OUTPUT,

	@TenPhim nvarchar (MAX)  ,

	@LichChieuBatDau datetime   ,

	@MoTa nvarchar (MAX)  ,

	@TrangThai bit   ,

	@LichChieuKetThuc datetime   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				INSERT INTO [dbo].[phim]
					(
					[TenPhim]
					,[LichChieuBatDau]
					,[MoTa]
					,[TrangThai]
					,[LichChieuKetThuc]
					)
				VALUES
					(
					@TenPhim
					,@LichChieuBatDau
					,@MoTa
					,@TrangThai
					,@LichChieuKetThuc
					)
				
				-- Get the identity value
				SET @PhimId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.phim_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.phim_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.phim_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the phim table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.phim_Update
(

	@PhimId int   ,

	@TenPhim nvarchar (MAX)  ,

	@LichChieuBatDau datetime   ,

	@MoTa nvarchar (MAX)  ,

	@TrangThai bit   ,

	@LichChieuKetThuc datetime   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[phim]
				SET
					[TenPhim] = @TenPhim
					,[LichChieuBatDau] = @LichChieuBatDau
					,[MoTa] = @MoTa
					,[TrangThai] = @TrangThai
					,[LichChieuKetThuc] = @LichChieuKetThuc
				WHERE
[PhimID] = @PhimId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.phim_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.phim_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.phim_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the phim table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.phim_Delete
(

	@PhimId int   
)
AS


				DELETE FROM [dbo].[phim] WITH (ROWLOCK) 
				WHERE
					[PhimID] = @PhimId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.phim_GetByPhimId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.phim_GetByPhimId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.phim_GetByPhimId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the phim table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.phim_GetByPhimId
(

	@PhimId int   
)
AS


				SELECT
					[PhimID],
					[TenPhim],
					[LichChieuBatDau],
					[MoTa],
					[TrangThai],
					[LichChieuKetThuc]
				FROM
					[dbo].[phim]
				WHERE
					[PhimID] = @PhimId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.phim_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.phim_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.phim_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the phim table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.phim_Find
(

	@SearchUsingOR bit   = null ,

	@PhimId int   = null ,

	@TenPhim nvarchar (MAX)  = null ,

	@LichChieuBatDau datetime   = null ,

	@MoTa nvarchar (MAX)  = null ,

	@TrangThai bit   = null ,

	@LichChieuKetThuc datetime   = null 
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [PhimID]
	, [TenPhim]
	, [LichChieuBatDau]
	, [MoTa]
	, [TrangThai]
	, [LichChieuKetThuc]
    FROM
	[dbo].[phim]
    WHERE 
	 ([PhimID] = @PhimId OR @PhimId IS NULL)
	AND ([TenPhim] = @TenPhim OR @TenPhim IS NULL)
	AND ([LichChieuBatDau] = @LichChieuBatDau OR @LichChieuBatDau IS NULL)
	AND ([MoTa] = @MoTa OR @MoTa IS NULL)
	AND ([TrangThai] = @TrangThai OR @TrangThai IS NULL)
	AND ([LichChieuKetThuc] = @LichChieuKetThuc OR @LichChieuKetThuc IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [PhimID]
	, [TenPhim]
	, [LichChieuBatDau]
	, [MoTa]
	, [TrangThai]
	, [LichChieuKetThuc]
    FROM
	[dbo].[phim]
    WHERE 
	 ([PhimID] = @PhimId AND @PhimId is not null)
	OR ([TenPhim] = @TenPhim AND @TenPhim is not null)
	OR ([LichChieuBatDau] = @LichChieuBatDau AND @LichChieuBatDau is not null)
	OR ([MoTa] = @MoTa AND @MoTa is not null)
	OR ([TrangThai] = @TrangThai AND @TrangThai is not null)
	OR ([LichChieuKetThuc] = @LichChieuKetThuc AND @LichChieuKetThuc is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.rap_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.rap_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.rap_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the rap table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.rap_Get_List

AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[RapID],
					[TenRap],
					[MoTa],
					[TrangThai]
				FROM
					[dbo].[rap]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.rap_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.rap_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.rap_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the rap table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.rap_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [RapID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([RapID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [RapID]'
				SET @SQL = @SQL + ' FROM [dbo].[rap]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[RapID], O.[TenRap], O.[MoTa], O.[TrangThai]
				FROM
				    [dbo].[rap] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[RapID] = PageIndex.[RapID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[rap]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.rap_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.rap_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.rap_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the rap table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.rap_Insert
(

	@RapId int    OUTPUT,

	@TenRap nvarchar (MAX)  ,

	@MoTa nvarchar (MAX)  ,

	@TrangThai bit   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				INSERT INTO [dbo].[rap]
					(
					[TenRap]
					,[MoTa]
					,[TrangThai]
					)
				VALUES
					(
					@TenRap
					,@MoTa
					,@TrangThai
					)
				
				-- Get the identity value
				SET @RapId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.rap_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.rap_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.rap_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the rap table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.rap_Update
(

	@RapId int   ,

	@TenRap nvarchar (MAX)  ,

	@MoTa nvarchar (MAX)  ,

	@TrangThai bit   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[rap]
				SET
					[TenRap] = @TenRap
					,[MoTa] = @MoTa
					,[TrangThai] = @TrangThai
				WHERE
[RapID] = @RapId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.rap_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.rap_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.rap_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the rap table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.rap_Delete
(

	@RapId int   
)
AS


				DELETE FROM [dbo].[rap] WITH (ROWLOCK) 
				WHERE
					[RapID] = @RapId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.rap_GetByRapId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.rap_GetByRapId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.rap_GetByRapId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the rap table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.rap_GetByRapId
(

	@RapId int   
)
AS


				SELECT
					[RapID],
					[TenRap],
					[MoTa],
					[TrangThai]
				FROM
					[dbo].[rap]
				WHERE
					[RapID] = @RapId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.rap_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.rap_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.rap_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the rap table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.rap_Find
(

	@SearchUsingOR bit   = null ,

	@RapId int   = null ,

	@TenRap nvarchar (MAX)  = null ,

	@MoTa nvarchar (MAX)  = null ,

	@TrangThai bit   = null 
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [RapID]
	, [TenRap]
	, [MoTa]
	, [TrangThai]
    FROM
	[dbo].[rap]
    WHERE 
	 ([RapID] = @RapId OR @RapId IS NULL)
	AND ([TenRap] = @TenRap OR @TenRap IS NULL)
	AND ([MoTa] = @MoTa OR @MoTa IS NULL)
	AND ([TrangThai] = @TrangThai OR @TrangThai IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [RapID]
	, [TenRap]
	, [MoTa]
	, [TrangThai]
    FROM
	[dbo].[rap]
    WHERE 
	 ([RapID] = @RapId AND @RapId is not null)
	OR ([TenRap] = @TenRap AND @TenRap is not null)
	OR ([MoTa] = @MoTa AND @MoTa is not null)
	OR ([TrangThai] = @TrangThai AND @TrangThai is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.user_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.user_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.user_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the user table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.user_Get_List

AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[UserID],
					[TenDangNhap],
					[MatKhau],
					[TenHienThi],
					[Email],
					[SoDienThoai],
					[SoCMTND],
					[DiaChi]
				FROM
					[dbo].[user]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.user_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.user_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.user_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the user table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.user_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [UserID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([UserID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [UserID]'
				SET @SQL = @SQL + ' FROM [dbo].[user]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[UserID], O.[TenDangNhap], O.[MatKhau], O.[TenHienThi], O.[Email], O.[SoDienThoai], O.[SoCMTND], O.[DiaChi]
				FROM
				    [dbo].[user] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[UserID] = PageIndex.[UserID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[user]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.user_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.user_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.user_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the user table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.user_Insert
(

	@UserId int    OUTPUT,

	@TenDangNhap nvarchar (MAX)  ,

	@MatKhau nvarchar (MAX)  ,

	@TenHienThi nvarchar (MAX)  ,

	@Email nvarchar (MAX)  ,

	@SoDienThoai varchar (50)  ,

	@SoCmtnd varchar (50)  ,

	@DiaChi nvarchar (MAX)  
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				INSERT INTO [dbo].[user]
					(
					[TenDangNhap]
					,[MatKhau]
					,[TenHienThi]
					,[Email]
					,[SoDienThoai]
					,[SoCMTND]
					,[DiaChi]
					)
				VALUES
					(
					@TenDangNhap
					,@MatKhau
					,@TenHienThi
					,@Email
					,@SoDienThoai
					,@SoCmtnd
					,@DiaChi
					)
				
				-- Get the identity value
				SET @UserId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.user_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.user_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.user_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the user table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.user_Update
(

	@UserId int   ,

	@TenDangNhap nvarchar (MAX)  ,

	@MatKhau nvarchar (MAX)  ,

	@TenHienThi nvarchar (MAX)  ,

	@Email nvarchar (MAX)  ,

	@SoDienThoai varchar (50)  ,

	@SoCmtnd varchar (50)  ,

	@DiaChi nvarchar (MAX)  
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[user]
				SET
					[TenDangNhap] = @TenDangNhap
					,[MatKhau] = @MatKhau
					,[TenHienThi] = @TenHienThi
					,[Email] = @Email
					,[SoDienThoai] = @SoDienThoai
					,[SoCMTND] = @SoCmtnd
					,[DiaChi] = @DiaChi
				WHERE
[UserID] = @UserId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.user_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.user_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.user_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the user table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.user_Delete
(

	@UserId int   
)
AS


				DELETE FROM [dbo].[user] WITH (ROWLOCK) 
				WHERE
					[UserID] = @UserId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.user_GetByUserId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.user_GetByUserId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.user_GetByUserId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the user table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.user_GetByUserId
(

	@UserId int   
)
AS


				SELECT
					[UserID],
					[TenDangNhap],
					[MatKhau],
					[TenHienThi],
					[Email],
					[SoDienThoai],
					[SoCMTND],
					[DiaChi]
				FROM
					[dbo].[user]
				WHERE
					[UserID] = @UserId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.user_GetByVeIdFromLichSuGdkh procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.user_GetByVeIdFromLichSuGdkh') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.user_GetByVeIdFromLichSuGdkh
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records through a junction table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.user_GetByVeIdFromLichSuGdkh
(

	@VeId int   
)
AS


SELECT dbo.[user].[UserID]
       ,dbo.[user].[TenDangNhap]
       ,dbo.[user].[MatKhau]
       ,dbo.[user].[TenHienThi]
       ,dbo.[user].[Email]
       ,dbo.[user].[SoDienThoai]
       ,dbo.[user].[SoCMTND]
       ,dbo.[user].[DiaChi]
  FROM dbo.[user]
 WHERE EXISTS (SELECT 1
                 FROM dbo.[lich_su_GDKH] 
                WHERE dbo.[lich_su_GDKH].[VeID] = @VeId
                  AND dbo.[lich_su_GDKH].[UserID] = dbo.[user].[UserID]
                  )
				SELECT @@ROWCOUNT			
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.user_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.user_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.user_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the user table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.user_Find
(

	@SearchUsingOR bit   = null ,

	@UserId int   = null ,

	@TenDangNhap nvarchar (MAX)  = null ,

	@MatKhau nvarchar (MAX)  = null ,

	@TenHienThi nvarchar (MAX)  = null ,

	@Email nvarchar (MAX)  = null ,

	@SoDienThoai varchar (50)  = null ,

	@SoCmtnd varchar (50)  = null ,

	@DiaChi nvarchar (MAX)  = null 
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [UserID]
	, [TenDangNhap]
	, [MatKhau]
	, [TenHienThi]
	, [Email]
	, [SoDienThoai]
	, [SoCMTND]
	, [DiaChi]
    FROM
	[dbo].[user]
    WHERE 
	 ([UserID] = @UserId OR @UserId IS NULL)
	AND ([TenDangNhap] = @TenDangNhap OR @TenDangNhap IS NULL)
	AND ([MatKhau] = @MatKhau OR @MatKhau IS NULL)
	AND ([TenHienThi] = @TenHienThi OR @TenHienThi IS NULL)
	AND ([Email] = @Email OR @Email IS NULL)
	AND ([SoDienThoai] = @SoDienThoai OR @SoDienThoai IS NULL)
	AND ([SoCMTND] = @SoCmtnd OR @SoCmtnd IS NULL)
	AND ([DiaChi] = @DiaChi OR @DiaChi IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [UserID]
	, [TenDangNhap]
	, [MatKhau]
	, [TenHienThi]
	, [Email]
	, [SoDienThoai]
	, [SoCMTND]
	, [DiaChi]
    FROM
	[dbo].[user]
    WHERE 
	 ([UserID] = @UserId AND @UserId is not null)
	OR ([TenDangNhap] = @TenDangNhap AND @TenDangNhap is not null)
	OR ([MatKhau] = @MatKhau AND @MatKhau is not null)
	OR ([TenHienThi] = @TenHienThi AND @TenHienThi is not null)
	OR ([Email] = @Email AND @Email is not null)
	OR ([SoDienThoai] = @SoDienThoai AND @SoDienThoai is not null)
	OR ([SoCMTND] = @SoCmtnd AND @SoCmtnd is not null)
	OR ([DiaChi] = @DiaChi AND @DiaChi is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.admin_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.admin_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.admin_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the admin table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.admin_Get_List

AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[AdminID],
					[TenDangNhap],
					[MatKhau],
					[TenHienThi]
				FROM
					[dbo].[admin]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.admin_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.admin_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.admin_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the admin table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.admin_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [AdminID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([AdminID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [AdminID]'
				SET @SQL = @SQL + ' FROM [dbo].[admin]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[AdminID], O.[TenDangNhap], O.[MatKhau], O.[TenHienThi]
				FROM
				    [dbo].[admin] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[AdminID] = PageIndex.[AdminID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[admin]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.admin_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.admin_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.admin_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the admin table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.admin_Insert
(

	@AdminId int    OUTPUT,

	@TenDangNhap nvarchar (MAX)  ,

	@MatKhau nvarchar (MAX)  ,

	@TenHienThi nvarchar (MAX)  
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				INSERT INTO [dbo].[admin]
					(
					[TenDangNhap]
					,[MatKhau]
					,[TenHienThi]
					)
				VALUES
					(
					@TenDangNhap
					,@MatKhau
					,@TenHienThi
					)
				
				-- Get the identity value
				SET @AdminId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.admin_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.admin_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.admin_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the admin table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.admin_Update
(

	@AdminId int   ,

	@TenDangNhap nvarchar (MAX)  ,

	@MatKhau nvarchar (MAX)  ,

	@TenHienThi nvarchar (MAX)  
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[admin]
				SET
					[TenDangNhap] = @TenDangNhap
					,[MatKhau] = @MatKhau
					,[TenHienThi] = @TenHienThi
				WHERE
[AdminID] = @AdminId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.admin_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.admin_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.admin_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the admin table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.admin_Delete
(

	@AdminId int   
)
AS


				DELETE FROM [dbo].[admin] WITH (ROWLOCK) 
				WHERE
					[AdminID] = @AdminId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.admin_GetByAdminId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.admin_GetByAdminId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.admin_GetByAdminId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the admin table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.admin_GetByAdminId
(

	@AdminId int   
)
AS


				SELECT
					[AdminID],
					[TenDangNhap],
					[MatKhau],
					[TenHienThi]
				FROM
					[dbo].[admin]
				WHERE
					[AdminID] = @AdminId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.admin_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.admin_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.admin_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the admin table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.admin_Find
(

	@SearchUsingOR bit   = null ,

	@AdminId int   = null ,

	@TenDangNhap nvarchar (MAX)  = null ,

	@MatKhau nvarchar (MAX)  = null ,

	@TenHienThi nvarchar (MAX)  = null 
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [AdminID]
	, [TenDangNhap]
	, [MatKhau]
	, [TenHienThi]
    FROM
	[dbo].[admin]
    WHERE 
	 ([AdminID] = @AdminId OR @AdminId IS NULL)
	AND ([TenDangNhap] = @TenDangNhap OR @TenDangNhap IS NULL)
	AND ([MatKhau] = @MatKhau OR @MatKhau IS NULL)
	AND ([TenHienThi] = @TenHienThi OR @TenHienThi IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [AdminID]
	, [TenDangNhap]
	, [MatKhau]
	, [TenHienThi]
    FROM
	[dbo].[admin]
    WHERE 
	 ([AdminID] = @AdminId AND @AdminId is not null)
	OR ([TenDangNhap] = @TenDangNhap AND @TenDangNhap is not null)
	OR ([MatKhau] = @MatKhau AND @MatKhau is not null)
	OR ([TenHienThi] = @TenHienThi AND @TenHienThi is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ghe_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ghe_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ghe_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the ghe table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ghe_Get_List

AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[GheID],
					[TenGhe],
					[LoaiVeID],
					[MoTa],
					[TrangThai]
				FROM
					[dbo].[ghe]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ghe_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ghe_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ghe_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the ghe table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ghe_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [GheID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([GheID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [GheID]'
				SET @SQL = @SQL + ' FROM [dbo].[ghe]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[GheID], O.[TenGhe], O.[LoaiVeID], O.[MoTa], O.[TrangThai]
				FROM
				    [dbo].[ghe] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[GheID] = PageIndex.[GheID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ghe]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ghe_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ghe_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ghe_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the ghe table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ghe_Insert
(

	@GheId int    OUTPUT,

	@TenGhe nvarchar (MAX)  ,

	@LoaiVeId int   ,

	@MoTa varchar (MAX)  ,

	@TrangThai bit   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				INSERT INTO [dbo].[ghe]
					(
					[TenGhe]
					,[LoaiVeID]
					,[MoTa]
					,[TrangThai]
					)
				VALUES
					(
					@TenGhe
					,@LoaiVeId
					,@MoTa
					,@TrangThai
					)
				
				-- Get the identity value
				SET @GheId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ghe_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ghe_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ghe_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the ghe table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ghe_Update
(

	@GheId int   ,

	@TenGhe nvarchar (MAX)  ,

	@LoaiVeId int   ,

	@MoTa varchar (MAX)  ,

	@TrangThai bit   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ghe]
				SET
					[TenGhe] = @TenGhe
					,[LoaiVeID] = @LoaiVeId
					,[MoTa] = @MoTa
					,[TrangThai] = @TrangThai
				WHERE
[GheID] = @GheId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ghe_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ghe_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ghe_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the ghe table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ghe_Delete
(

	@GheId int   
)
AS


				DELETE FROM [dbo].[ghe] WITH (ROWLOCK) 
				WHERE
					[GheID] = @GheId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ghe_GetByGheId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ghe_GetByGheId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ghe_GetByGheId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the ghe table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ghe_GetByGheId
(

	@GheId int   
)
AS


				SELECT
					[GheID],
					[TenGhe],
					[LoaiVeID],
					[MoTa],
					[TrangThai]
				FROM
					[dbo].[ghe]
				WHERE
					[GheID] = @GheId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ghe_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ghe_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ghe_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the ghe table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ghe_Find
(

	@SearchUsingOR bit   = null ,

	@GheId int   = null ,

	@TenGhe nvarchar (MAX)  = null ,

	@LoaiVeId int   = null ,

	@MoTa varchar (MAX)  = null ,

	@TrangThai bit   = null 
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [GheID]
	, [TenGhe]
	, [LoaiVeID]
	, [MoTa]
	, [TrangThai]
    FROM
	[dbo].[ghe]
    WHERE 
	 ([GheID] = @GheId OR @GheId IS NULL)
	AND ([TenGhe] = @TenGhe OR @TenGhe IS NULL)
	AND ([LoaiVeID] = @LoaiVeId OR @LoaiVeId IS NULL)
	AND ([MoTa] = @MoTa OR @MoTa IS NULL)
	AND ([TrangThai] = @TrangThai OR @TrangThai IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [GheID]
	, [TenGhe]
	, [LoaiVeID]
	, [MoTa]
	, [TrangThai]
    FROM
	[dbo].[ghe]
    WHERE 
	 ([GheID] = @GheId AND @GheId is not null)
	OR ([TenGhe] = @TenGhe AND @TenGhe is not null)
	OR ([LoaiVeID] = @LoaiVeId AND @LoaiVeId is not null)
	OR ([MoTa] = @MoTa AND @MoTa is not null)
	OR ([TrangThai] = @TrangThai AND @TrangThai is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.phong_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.phong_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.phong_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the phong table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.phong_Get_List

AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[PhongID],
					[TenPhong],
					[MoTa],
					[TrangThai]
				FROM
					[dbo].[phong]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.phong_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.phong_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.phong_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the phong table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.phong_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [PhongID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([PhongID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [PhongID]'
				SET @SQL = @SQL + ' FROM [dbo].[phong]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[PhongID], O.[TenPhong], O.[MoTa], O.[TrangThai]
				FROM
				    [dbo].[phong] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[PhongID] = PageIndex.[PhongID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[phong]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.phong_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.phong_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.phong_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the phong table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.phong_Insert
(

	@PhongId int    OUTPUT,

	@TenPhong int   ,

	@MoTa nvarchar (MAX)  ,

	@TrangThai bit   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				INSERT INTO [dbo].[phong]
					(
					[TenPhong]
					,[MoTa]
					,[TrangThai]
					)
				VALUES
					(
					@TenPhong
					,@MoTa
					,@TrangThai
					)
				
				-- Get the identity value
				SET @PhongId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.phong_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.phong_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.phong_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the phong table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.phong_Update
(

	@PhongId int   ,

	@TenPhong int   ,

	@MoTa nvarchar (MAX)  ,

	@TrangThai bit   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[phong]
				SET
					[TenPhong] = @TenPhong
					,[MoTa] = @MoTa
					,[TrangThai] = @TrangThai
				WHERE
[PhongID] = @PhongId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.phong_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.phong_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.phong_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the phong table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.phong_Delete
(

	@PhongId int   
)
AS


				DELETE FROM [dbo].[phong] WITH (ROWLOCK) 
				WHERE
					[PhongID] = @PhongId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.phong_GetByPhongId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.phong_GetByPhongId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.phong_GetByPhongId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the phong table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.phong_GetByPhongId
(

	@PhongId int   
)
AS


				SELECT
					[PhongID],
					[TenPhong],
					[MoTa],
					[TrangThai]
				FROM
					[dbo].[phong]
				WHERE
					[PhongID] = @PhongId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.phong_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.phong_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.phong_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the phong table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.phong_Find
(

	@SearchUsingOR bit   = null ,

	@PhongId int   = null ,

	@TenPhong int   = null ,

	@MoTa nvarchar (MAX)  = null ,

	@TrangThai bit   = null 
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [PhongID]
	, [TenPhong]
	, [MoTa]
	, [TrangThai]
    FROM
	[dbo].[phong]
    WHERE 
	 ([PhongID] = @PhongId OR @PhongId IS NULL)
	AND ([TenPhong] = @TenPhong OR @TenPhong IS NULL)
	AND ([MoTa] = @MoTa OR @MoTa IS NULL)
	AND ([TrangThai] = @TrangThai OR @TrangThai IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [PhongID]
	, [TenPhong]
	, [MoTa]
	, [TrangThai]
    FROM
	[dbo].[phong]
    WHERE 
	 ([PhongID] = @PhongId AND @PhongId is not null)
	OR ([TenPhong] = @TenPhong AND @TenPhong is not null)
	OR ([MoTa] = @MoTa AND @MoTa is not null)
	OR ([TrangThai] = @TrangThai AND @TrangThai is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ve_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ve_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ve_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the ve table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ve_Get_List

AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[VeID],
					[TenVe],
					[MoTa],
					[TrangThai],
					[LoaiVeID],
					[RapID],
					[PhongID],
					[PhimID],
					[GheID],
					[Gia]
				FROM
					[dbo].[ve]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ve_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ve_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ve_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the ve table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ve_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [VeID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([VeID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [VeID]'
				SET @SQL = @SQL + ' FROM [dbo].[ve]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[VeID], O.[TenVe], O.[MoTa], O.[TrangThai], O.[LoaiVeID], O.[RapID], O.[PhongID], O.[PhimID], O.[GheID], O.[Gia]
				FROM
				    [dbo].[ve] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[VeID] = PageIndex.[VeID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[ve]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ve_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ve_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ve_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the ve table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ve_Insert
(

	@VeId int    OUTPUT,

	@TenVe varchar (MAX)  ,

	@MoTa nvarchar (MAX)  ,

	@TrangThai bit   ,

	@LoaiVeId int   ,

	@RapId int   ,

	@PhongId int   ,

	@PhimId int   ,

	@GheId int   ,

	@Gia decimal (18, 0)  
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				INSERT INTO [dbo].[ve]
					(
					[TenVe]
					,[MoTa]
					,[TrangThai]
					,[LoaiVeID]
					,[RapID]
					,[PhongID]
					,[PhimID]
					,[GheID]
					,[Gia]
					)
				VALUES
					(
					@TenVe
					,@MoTa
					,@TrangThai
					,@LoaiVeId
					,@RapId
					,@PhongId
					,@PhimId
					,@GheId
					,@Gia
					)
				
				-- Get the identity value
				SET @VeId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ve_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ve_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ve_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the ve table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ve_Update
(

	@VeId int   ,

	@TenVe varchar (MAX)  ,

	@MoTa nvarchar (MAX)  ,

	@TrangThai bit   ,

	@LoaiVeId int   ,

	@RapId int   ,

	@PhongId int   ,

	@PhimId int   ,

	@GheId int   ,

	@Gia decimal (18, 0)  
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[ve]
				SET
					[TenVe] = @TenVe
					,[MoTa] = @MoTa
					,[TrangThai] = @TrangThai
					,[LoaiVeID] = @LoaiVeId
					,[RapID] = @RapId
					,[PhongID] = @PhongId
					,[PhimID] = @PhimId
					,[GheID] = @GheId
					,[Gia] = @Gia
				WHERE
[VeID] = @VeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ve_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ve_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ve_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the ve table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ve_Delete
(

	@VeId int   
)
AS


				DELETE FROM [dbo].[ve] WITH (ROWLOCK) 
				WHERE
					[VeID] = @VeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ve_GetByGheId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ve_GetByGheId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ve_GetByGheId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the ve table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ve_GetByGheId
(

	@GheId int   
)
AS


				SET ANSI_NULLS OFF
				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[VeID],
					[TenVe],
					[MoTa],
					[TrangThai],
					[LoaiVeID],
					[RapID],
					[PhongID],
					[PhimID],
					[GheID],
					[Gia]
				FROM
					[dbo].[ve]
				WHERE
					[GheID] = @GheId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ve_GetByLoaiVeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ve_GetByLoaiVeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ve_GetByLoaiVeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the ve table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ve_GetByLoaiVeId
(

	@LoaiVeId int   
)
AS


				SET ANSI_NULLS OFF
				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[VeID],
					[TenVe],
					[MoTa],
					[TrangThai],
					[LoaiVeID],
					[RapID],
					[PhongID],
					[PhimID],
					[GheID],
					[Gia]
				FROM
					[dbo].[ve]
				WHERE
					[LoaiVeID] = @LoaiVeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ve_GetByPhimId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ve_GetByPhimId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ve_GetByPhimId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the ve table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ve_GetByPhimId
(

	@PhimId int   
)
AS


				SET ANSI_NULLS OFF
				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[VeID],
					[TenVe],
					[MoTa],
					[TrangThai],
					[LoaiVeID],
					[RapID],
					[PhongID],
					[PhimID],
					[GheID],
					[Gia]
				FROM
					[dbo].[ve]
				WHERE
					[PhimID] = @PhimId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ve_GetByPhongId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ve_GetByPhongId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ve_GetByPhongId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the ve table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ve_GetByPhongId
(

	@PhongId int   
)
AS


				SET ANSI_NULLS OFF
				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[VeID],
					[TenVe],
					[MoTa],
					[TrangThai],
					[LoaiVeID],
					[RapID],
					[PhongID],
					[PhimID],
					[GheID],
					[Gia]
				FROM
					[dbo].[ve]
				WHERE
					[PhongID] = @PhongId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ve_GetByRapId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ve_GetByRapId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ve_GetByRapId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the ve table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ve_GetByRapId
(

	@RapId int   
)
AS


				SET ANSI_NULLS OFF
				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[VeID],
					[TenVe],
					[MoTa],
					[TrangThai],
					[LoaiVeID],
					[RapID],
					[PhongID],
					[PhimID],
					[GheID],
					[Gia]
				FROM
					[dbo].[ve]
				WHERE
					[RapID] = @RapId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ve_GetByVeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ve_GetByVeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ve_GetByVeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the ve table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ve_GetByVeId
(

	@VeId int   
)
AS


				SELECT
					[VeID],
					[TenVe],
					[MoTa],
					[TrangThai],
					[LoaiVeID],
					[RapID],
					[PhongID],
					[PhimID],
					[GheID],
					[Gia]
				FROM
					[dbo].[ve]
				WHERE
					[VeID] = @VeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ve_GetByUserIdFromLichSuGdkh procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ve_GetByUserIdFromLichSuGdkh') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ve_GetByUserIdFromLichSuGdkh
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records through a junction table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ve_GetByUserIdFromLichSuGdkh
(

	@UserId int   
)
AS


SELECT dbo.[ve].[VeID]
       ,dbo.[ve].[TenVe]
       ,dbo.[ve].[MoTa]
       ,dbo.[ve].[TrangThai]
       ,dbo.[ve].[LoaiVeID]
       ,dbo.[ve].[RapID]
       ,dbo.[ve].[PhongID]
       ,dbo.[ve].[PhimID]
       ,dbo.[ve].[GheID]
       ,dbo.[ve].[Gia]
  FROM dbo.[ve]
 WHERE EXISTS (SELECT 1
                 FROM dbo.[lich_su_GDKH] 
                WHERE dbo.[lich_su_GDKH].[UserID] = @UserId
                  AND dbo.[lich_su_GDKH].[VeID] = dbo.[ve].[VeID]
                  )
				SELECT @@ROWCOUNT			
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.ve_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.ve_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.ve_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the ve table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.ve_Find
(

	@SearchUsingOR bit   = null ,

	@VeId int   = null ,

	@TenVe varchar (MAX)  = null ,

	@MoTa nvarchar (MAX)  = null ,

	@TrangThai bit   = null ,

	@LoaiVeId int   = null ,

	@RapId int   = null ,

	@PhongId int   = null ,

	@PhimId int   = null ,

	@GheId int   = null ,

	@Gia decimal (18, 0)  = null 
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [VeID]
	, [TenVe]
	, [MoTa]
	, [TrangThai]
	, [LoaiVeID]
	, [RapID]
	, [PhongID]
	, [PhimID]
	, [GheID]
	, [Gia]
    FROM
	[dbo].[ve]
    WHERE 
	 ([VeID] = @VeId OR @VeId IS NULL)
	AND ([TenVe] = @TenVe OR @TenVe IS NULL)
	AND ([MoTa] = @MoTa OR @MoTa IS NULL)
	AND ([TrangThai] = @TrangThai OR @TrangThai IS NULL)
	AND ([LoaiVeID] = @LoaiVeId OR @LoaiVeId IS NULL)
	AND ([RapID] = @RapId OR @RapId IS NULL)
	AND ([PhongID] = @PhongId OR @PhongId IS NULL)
	AND ([PhimID] = @PhimId OR @PhimId IS NULL)
	AND ([GheID] = @GheId OR @GheId IS NULL)
	AND ([Gia] = @Gia OR @Gia IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [VeID]
	, [TenVe]
	, [MoTa]
	, [TrangThai]
	, [LoaiVeID]
	, [RapID]
	, [PhongID]
	, [PhimID]
	, [GheID]
	, [Gia]
    FROM
	[dbo].[ve]
    WHERE 
	 ([VeID] = @VeId AND @VeId is not null)
	OR ([TenVe] = @TenVe AND @TenVe is not null)
	OR ([MoTa] = @MoTa AND @MoTa is not null)
	OR ([TrangThai] = @TrangThai AND @TrangThai is not null)
	OR ([LoaiVeID] = @LoaiVeId AND @LoaiVeId is not null)
	OR ([RapID] = @RapId AND @RapId is not null)
	OR ([PhongID] = @PhongId AND @PhongId is not null)
	OR ([PhimID] = @PhimId AND @PhimId is not null)
	OR ([GheID] = @GheId AND @GheId is not null)
	OR ([Gia] = @Gia AND @Gia is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.lich_su_GDKH_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.lich_su_GDKH_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.lich_su_GDKH_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the lich_su_GDKH table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.lich_su_GDKH_Get_List

AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[UserID],
					[VeID],
					[NgayDatVe],
					[SoLanDat]
				FROM
					[dbo].[lich_su_GDKH]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.lich_su_GDKH_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.lich_su_GDKH_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.lich_su_GDKH_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the lich_su_GDKH table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.lich_su_GDKH_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [UserID] int, [VeID] int 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([UserID], [VeID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [UserID], [VeID]'
				SET @SQL = @SQL + ' FROM [dbo].[lich_su_GDKH]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[UserID], O.[VeID], O.[NgayDatVe], O.[SoLanDat]
				FROM
				    [dbo].[lich_su_GDKH] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[UserID] = PageIndex.[UserID]
					AND O.[VeID] = PageIndex.[VeID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[lich_su_GDKH]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.lich_su_GDKH_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.lich_su_GDKH_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.lich_su_GDKH_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the lich_su_GDKH table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.lich_su_GDKH_Insert
(

	@UserId int   ,

	@VeId int   ,

	@NgayDatVe datetime   ,

	@SoLanDat int   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				INSERT INTO [dbo].[lich_su_GDKH]
					(
					[UserID]
					,[VeID]
					,[NgayDatVe]
					,[SoLanDat]
					)
				VALUES
					(
					@UserId
					,@VeId
					,@NgayDatVe
					,@SoLanDat
					)
				
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.lich_su_GDKH_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.lich_su_GDKH_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.lich_su_GDKH_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the lich_su_GDKH table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.lich_su_GDKH_Update
(

	@UserId int   ,

	@OriginalUserId int   ,

	@VeId int   ,

	@OriginalVeId int   ,

	@NgayDatVe datetime   ,

	@SoLanDat int   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[lich_su_GDKH]
				SET
					[UserID] = @UserId
					,[VeID] = @VeId
					,[NgayDatVe] = @NgayDatVe
					,[SoLanDat] = @SoLanDat
				WHERE
[UserID] = @OriginalUserId 
AND [VeID] = @OriginalVeId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.lich_su_GDKH_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.lich_su_GDKH_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.lich_su_GDKH_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the lich_su_GDKH table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.lich_su_GDKH_Delete
(

	@UserId int   ,

	@VeId int   
)
AS


				DELETE FROM [dbo].[lich_su_GDKH] WITH (ROWLOCK) 
				WHERE
					[UserID] = @UserId
					AND [VeID] = @VeId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.lich_su_GDKH_GetByUserId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.lich_su_GDKH_GetByUserId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.lich_su_GDKH_GetByUserId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the lich_su_GDKH table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.lich_su_GDKH_GetByUserId
(

	@UserId int   
)
AS


				SET ANSI_NULLS OFF
				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[UserID],
					[VeID],
					[NgayDatVe],
					[SoLanDat]
				FROM
					[dbo].[lich_su_GDKH]
				WHERE
					[UserID] = @UserId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.lich_su_GDKH_GetByVeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.lich_su_GDKH_GetByVeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.lich_su_GDKH_GetByVeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the lich_su_GDKH table through a foreign key
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.lich_su_GDKH_GetByVeId
(

	@VeId int   
)
AS


				SET ANSI_NULLS OFF
				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[UserID],
					[VeID],
					[NgayDatVe],
					[SoLanDat]
				FROM
					[dbo].[lich_su_GDKH]
				WHERE
					[VeID] = @VeId
				
				SELECT @@ROWCOUNT
				SET ANSI_NULLS ON
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.lich_su_GDKH_GetByUserIdVeId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.lich_su_GDKH_GetByUserIdVeId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.lich_su_GDKH_GetByUserIdVeId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the lich_su_GDKH table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.lich_su_GDKH_GetByUserIdVeId
(

	@UserId int   ,

	@VeId int   
)
AS


				SELECT
					[UserID],
					[VeID],
					[NgayDatVe],
					[SoLanDat]
				FROM
					[dbo].[lich_su_GDKH]
				WHERE
					[UserID] = @UserId
					AND [VeID] = @VeId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.lich_su_GDKH_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.lich_su_GDKH_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.lich_su_GDKH_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the lich_su_GDKH table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.lich_su_GDKH_Find
(

	@SearchUsingOR bit   = null ,

	@UserId int   = null ,

	@VeId int   = null ,

	@NgayDatVe datetime   = null ,

	@SoLanDat int   = null 
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [UserID]
	, [VeID]
	, [NgayDatVe]
	, [SoLanDat]
    FROM
	[dbo].[lich_su_GDKH]
    WHERE 
	 ([UserID] = @UserId OR @UserId IS NULL)
	AND ([VeID] = @VeId OR @VeId IS NULL)
	AND ([NgayDatVe] = @NgayDatVe OR @NgayDatVe IS NULL)
	AND ([SoLanDat] = @SoLanDat OR @SoLanDat IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [UserID]
	, [VeID]
	, [NgayDatVe]
	, [SoLanDat]
    FROM
	[dbo].[lich_su_GDKH]
    WHERE 
	 ([UserID] = @UserId AND @UserId is not null)
	OR ([VeID] = @VeId AND @VeId is not null)
	OR ([NgayDatVe] = @NgayDatVe AND @NgayDatVe is not null)
	OR ([SoLanDat] = @SoLanDat AND @SoLanDat is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.GioChieu_Get_List procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.GioChieu_Get_List') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.GioChieu_Get_List
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets all records from the GioChieu table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.GioChieu_Get_List

AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				SELECT
					[GioChieuID],
					[TenGioChieu],
					[MoTa]
				FROM
					[dbo].[GioChieu]
					
				SELECT @@ROWCOUNT
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.GioChieu_GetPaged procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.GioChieu_GetPaged') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.GioChieu_GetPaged
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Gets records from the GioChieu table passing page index and page count parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.GioChieu_GetPaged
(

	@WhereClause varchar (2000)  ,

	@OrderBy varchar (2000)  ,

	@PageIndex int   ,

	@PageSize int   
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				BEGIN
				DECLARE @PageLowerBound int
				DECLARE @PageUpperBound int
				
				-- Set the page bounds
				SET @PageLowerBound = @PageSize * @PageIndex
				SET @PageUpperBound = @PageLowerBound + @PageSize

				-- Create a temp table to store the select results
				CREATE TABLE #PageIndex
				(
				    [IndexId] int IDENTITY (1, 1) NOT NULL,
				    [GioChieuID] decimal(18, 0) 
				)
				
				-- Insert into the temp table
				DECLARE @SQL AS nvarchar(4000)
				SET @SQL = 'INSERT INTO #PageIndex ([GioChieuID])'
				SET @SQL = @SQL + ' SELECT'
				IF @PageSize > 0
				BEGIN
					SET @SQL = @SQL + ' TOP ' + CONVERT(nvarchar, @PageUpperBound)
				END
				SET @SQL = @SQL + ' [GioChieuID]'
				SET @SQL = @SQL + ' FROM [dbo].[GioChieu]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				IF LEN(@OrderBy) > 0
				BEGIN
					SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
				END
				
				-- Populate the temp table
				EXEC sp_executesql @SQL

				-- Return paged results
				SELECT O.[GioChieuID], O.[TenGioChieu], O.[MoTa]
				FROM
				    [dbo].[GioChieu] O,
				    #PageIndex PageIndex
				WHERE
				    PageIndex.IndexId > @PageLowerBound
					AND O.[GioChieuID] = PageIndex.[GioChieuID]
				ORDER BY
				    PageIndex.IndexId
				
				-- get row count
				SET @SQL = 'SELECT COUNT(*) AS TotalRowCount'
				SET @SQL = @SQL + ' FROM [dbo].[GioChieu]'
				IF LEN(@WhereClause) > 0
				BEGIN
					SET @SQL = @SQL + ' WHERE ' + @WhereClause
				END
				EXEC sp_executesql @SQL
			
				END
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.GioChieu_Insert procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.GioChieu_Insert') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.GioChieu_Insert
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Inserts a record into the GioChieu table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.GioChieu_Insert
(

	@GioChieuId decimal (18, 0)   OUTPUT,

	@TenGioChieu nvarchar (250)  ,

	@MoTa nvarchar (MAX)  
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				INSERT INTO [dbo].[GioChieu]
					(
					[TenGioChieu]
					,[MoTa]
					)
				VALUES
					(
					@TenGioChieu
					,@MoTa
					)
				
				-- Get the identity value
				SET @GioChieuId = SCOPE_IDENTITY()
									
							
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.GioChieu_Update procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.GioChieu_Update') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.GioChieu_Update
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Updates a record in the GioChieu table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.GioChieu_Update
(

	@GioChieuId decimal (18, 0)  ,

	@TenGioChieu nvarchar (250)  ,

	@MoTa nvarchar (MAX)  
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
				
				-- Modify the updatable columns
				UPDATE
					[dbo].[GioChieu]
				SET
					[TenGioChieu] = @TenGioChieu
					,[MoTa] = @MoTa
				WHERE
[GioChieuID] = @GioChieuId 
				
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.GioChieu_Delete procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.GioChieu_Delete') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.GioChieu_Delete
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Deletes a record in the GioChieu table
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.GioChieu_Delete
(

	@GioChieuId decimal (18, 0)  
)
AS


				DELETE FROM [dbo].[GioChieu] WITH (ROWLOCK) 
				WHERE
					[GioChieuID] = @GioChieuId
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.GioChieu_GetByGioChieuId procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.GioChieu_GetByGioChieuId') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.GioChieu_GetByGioChieuId
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Select records from the GioChieu table through an index
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.GioChieu_GetByGioChieuId
(

	@GioChieuId decimal (18, 0)  
)
AS


				SELECT
					[GioChieuID],
					[TenGioChieu],
					[MoTa]
				FROM
					[dbo].[GioChieu]
				WHERE
					[GioChieuID] = @GioChieuId
				SELECT @@ROWCOUNT
					
			

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

	

-- Drop the dbo.GioChieu_Find procedure
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'dbo.GioChieu_Find') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE dbo.GioChieu_Find
GO

/*
----------------------------------------------------------------------------------------------------

-- Created By:  ()
-- Purpose: Finds records in the GioChieu table passing nullable parameters
----------------------------------------------------------------------------------------------------
*/


CREATE PROCEDURE dbo.GioChieu_Find
(

	@SearchUsingOR bit   = null ,

	@GioChieuId decimal (18, 0)  = null ,

	@TenGioChieu nvarchar (250)  = null ,

	@MoTa nvarchar (MAX)  = null 
)
AS


				SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
  IF ISNULL(@SearchUsingOR, 0) <> 1
  BEGIN
    SELECT
	  [GioChieuID]
	, [TenGioChieu]
	, [MoTa]
    FROM
	[dbo].[GioChieu]
    WHERE 
	 ([GioChieuID] = @GioChieuId OR @GioChieuId IS NULL)
	AND ([TenGioChieu] = @TenGioChieu OR @TenGioChieu IS NULL)
	AND ([MoTa] = @MoTa OR @MoTa IS NULL)
						
  END
  ELSE
  BEGIN
    SELECT
	  [GioChieuID]
	, [TenGioChieu]
	, [MoTa]
    FROM
	[dbo].[GioChieu]
    WHERE 
	 ([GioChieuID] = @GioChieuId AND @GioChieuId is not null)
	OR ([TenGioChieu] = @TenGioChieu AND @TenGioChieu is not null)
	OR ([MoTa] = @MoTa AND @MoTa is not null)
	SELECT @@ROWCOUNT			
  END
				

GO
SET QUOTED_IDENTIFIER ON 
GO
SET NOCOUNT ON
GO
SET ANSI_NULLS OFF 
GO

